/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client;

import org.ajiaojr.enigmagm.client.dialog.LoginDialog;
import org.ajiaojr.enigmagm.client.dialog.RegisterDialog;
import org.ajiaojr.enigmagm.client.event.UserEventAdapter;
import org.ajiaojr.enigmagm.client.section.AdministerSection;
import org.ajiaojr.enigmagm.client.section.AtAGlanceSection;
import org.ajiaojr.enigmagm.client.section.GuildRosterSection;
import org.ajiaojr.enigmagm.client.section.PointSystemsSection;
import org.ajiaojr.enigmagm.client.section.Section;
import org.ajiaojr.enigmagm.client.section.SectionList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Program entry point.
 * 
 * @author ajiaojr
 */
public class EnigmaGM implements EntryPoint {

  public static final BuildInfo BUILDINFO =
      (BuildInfo) GWT.create(BuildInfo.class);

  private static final EnigmaGMMessages MESSAGES =
      (EnigmaGMMessages) GWT.create(EnigmaGMMessages.class);

  public static String getVersion() {
    return MESSAGES.version(BUILDINFO.majorVersion(), BUILDINFO.minorVersion(),
        BUILDINFO.revision(), BUILDINFO.buildTime());
  }

  private HorizontalPanel headerLinksPanel;

  private Label loginLabel;

  private Label logoutLabel;

  private VerticalPanel mainPanel;

  private Label registerLabel;

  private SectionList sectionList = new SectionList();

  /*
   * (non-Javadoc)
   * 
   * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
   */
  public void onModuleLoad() {
    RootPanel rootPanel = RootPanel.get();
    Window.setTitle(MESSAGES.title());

    History.addHistoryListener(new HistoryListener() {

      public void onHistoryChanged(String historyToken) {
        Section section = sectionList.get(historyToken);

        if (section != null) {
          showSection(section, false);
        }
      }
    });

    final DockPanel dockPanel = new DockPanel();
    rootPanel.add(dockPanel);
    dockPanel.setWidth("100%");

    final VerticalPanel verticalPanel_2 = new VerticalPanel();
    dockPanel.add(verticalPanel_2, DockPanel.NORTH);
    verticalPanel_2.setWidth("100%");

    final HorizontalPanel headerPanel = new HorizontalPanel();
    verticalPanel_2.add(headerPanel);
    headerPanel.setWidth("100%");

    final HTML websiteLink = new HTML(MESSAGES.websiteLink(MESSAGES.website()));
    headerPanel.add(websiteLink);
    headerPanel.setCellVerticalAlignment(websiteLink,
        HasVerticalAlignment.ALIGN_MIDDLE);

    headerLinksPanel = new HorizontalPanel();
    headerPanel.add(headerLinksPanel);
    headerLinksPanel.setSpacing(5);
    headerPanel.setCellHorizontalAlignment(headerLinksPanel,
        HasHorizontalAlignment.ALIGN_RIGHT);

    registerLabel = new Label(MESSAGES.register());
    headerLinksPanel.add(registerLabel);
    registerLabel.addClickListener(new ClickListener() {

      public void onClick(Widget sender) {
        new RegisterDialog().show();
      }
    });
    registerLabel.setStyleName("label-Link");

    loginLabel = new Label(MESSAGES.login());
    headerLinksPanel.add(loginLabel);
    loginLabel.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        new LoginDialog().show();
      }
    });
    loginLabel.setStyleName("label-Link");

    final VerticalPanel verticalPanel_3 = new VerticalPanel();
    dockPanel.add(verticalPanel_3, DockPanel.CENTER);
    verticalPanel_3.setWidth("100%");

    mainPanel = new VerticalPanel();
    verticalPanel_3.add(mainPanel);
    mainPanel.setWidth("100%");
    mainPanel.setStyleName("mainPanel");

    logoutLabel = new Label(MESSAGES.logout());
    logoutLabel.setStyleName("label-Link");
    logoutLabel.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        UserSessionManager.getInstance().logout();
      }
    });

    final VerticalPanel verticalPanel = new VerticalPanel();
    dockPanel.add(verticalPanel, DockPanel.WEST);
    dockPanel.setCellWidth(verticalPanel, "160px");
    verticalPanel.setWidth("100%");
    verticalPanel.add(sectionList);

    sectionList.setStyleName("section-list");
    sectionList.setWidth("100%");

    final VerticalPanel verticalPanel_1 = new VerticalPanel();
    rootPanel.add(verticalPanel_1);
    verticalPanel_1.setSpacing(5);
    verticalPanel_1.setWidth("100%");

    final HTML html = new HTML(MESSAGES.views());
    verticalPanel_1.add(html);
    verticalPanel_1.setCellHorizontalAlignment(html,
        HasHorizontalAlignment.ALIGN_CENTER);

    final HTML footer = new HTML(MESSAGES.footer());
    verticalPanel_1.add(footer);
    verticalPanel_1.setCellHorizontalAlignment(footer,
        HasHorizontalAlignment.ALIGN_CENTER);

    final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
    verticalPanel_1.add(horizontalPanel_1);
    horizontalPanel_1.setSpacing(5);
    verticalPanel_1.setCellHorizontalAlignment(horizontalPanel_1,
        HasHorizontalAlignment.ALIGN_CENTER);

    final Label versionLabel = new Label();
    horizontalPanel_1.add(versionLabel);
    versionLabel.setText(getVersion());

    UserSessionManager.getInstance().addUserEventListener(
        new UserEventAdapter() {

          public void onLoginFailure(Throwable cause) {
            Window.alert(cause.getMessage());
          }

          public void onLoginSuccess() {
            headerLinksPanel.remove(registerLabel);
            headerLinksPanel.remove(loginLabel);
            headerLinksPanel.add(logoutLabel);
            if (UserSessionManager.getInstance().getUser().isAdmin()) {
              sectionList.addSection(AdministerSection.getInstance());
            }
          }

          public void onLogoutFailure(Throwable cause) {
            Window.alert(cause.getMessage());
          }

          public void onLogoutSuccess() {
            headerLinksPanel.remove(logoutLabel);
            headerLinksPanel.add(registerLabel);
            headerLinksPanel.add(loginLabel);
            showSection(PointSystemsSection.getInstance(), true);
            sectionList.removeSection(AdministerSection.getInstance());
          }
        });

    initSections();

    UserSessionManager.getInstance().loginViaCookie();
  }

  private void initSections() {
    // sectionList.addSection(AtAGlanceSection.getInstance());
    sectionList.addSection(PointSystemsSection.getInstance());
    sectionList.addSection(GuildRosterSection.getInstance());
    showSection(PointSystemsSection.getInstance(), true);
    // showSection(AtAGlanceSection.getInstance(), true);
  }

  private void showSection(Section section, boolean changeHistory) {
    if (section != sectionList.getCurrentSection()) {
      if (sectionList.getCurrentSection() != null) {
        mainPanel.remove(sectionList.getCurrentSection());
      }

      sectionList.setSelection(section);
      mainPanel.add(section);
      mainPanel.setCellWidth(section, "100%");
      mainPanel.setCellHeight(section, "100%");
      mainPanel.setCellVerticalAlignment(section, VerticalPanel.ALIGN_TOP);

      if (changeHistory) {
        History.newItem(section.getName());
      }
    }
  }
}
